package com.hk.webapp.controller.admin;

import com.hk.webapp.annotations.SkipPermission;
import com.hk.webapp.bean.*;
import com.hk.webapp.service.SysActionService;
import com.hk.webapp.service.SysRoleActionService;
import com.hk.webapp.service.SysUserRoleService;
import com.hk.webapp.service.SysUserService;
import com.hk.webapp.vo.ResultVo;
import com.hk.webapp.vo.SysActionVo;
import com.hk.webapp.vo.UpdateActionVo;
import com.google.common.collect.Maps;
import com.hk.webapp.bean.SysAction;
import com.hk.webapp.bean.SysRoleAction;
import com.hk.webapp.bean.SysUserRole;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.*;
import java.util.stream.Collectors;

@RestController
public class SysActionController extends AdminBaseController {

    @Resource
    private SysActionService sysActionService;
    @Resource
    private SysUserRoleService sysUserRoleService;
    @Resource
    private SysRoleActionService sysRoleActionService;
    @Resource
    private SysUserService sysUserService;

    @SkipPermission
    @PostMapping("/SysAction/GetUserAllAction")
    public ResultVo getUserAllAction(@RequestHeader(value = "userId", required = false) Integer userId) {
        //1.0 获取所有权限
        List<SysAction> allAction;
        if (userId == 1) {
            //超级管理员获取所有
            allAction = sysActionService.getEntityList();
        } else {
            //其他管理员
            //1.0 获取用户和角色关系
            List<SysUserRole> userRoleList = sysUserRoleService.getEntityListByUserId(userId);
            //2.0 获取所有角色的所有权限
            ArrayList<SysRoleAction> _all_sys_roleActions = new ArrayList<SysRoleAction>();
            for (SysUserRole item : userRoleList) {
                ArrayList<SysRoleAction> _list = sysRoleActionService.getEntityListByRoleId(item.getSysRoleId());
                if (_list.size() > 0) {
                    _all_sys_roleActions.addAll(_list);
                }
            }
            //3.0 合并所有权限
            List<Integer> sysActionIds = _all_sys_roleActions.parallelStream().distinct().map(SysRoleAction::getSysActionId).collect(Collectors.toList());


            //4.0 根据合并后的权限id数组 查询权限列表

            allAction = sysActionService.getEntityByIds(sysActionIds);

        }

        return ResultVo.createBySuccessData(allAction.parallelStream().filter(c->c.getActionTypeEnum()==1).collect(Collectors.toList()));
    }

    @PostMapping("/SysAction/GetAllAction")
    public ResultVo getAllAction(){
        List<SysAction> allAction = sysActionService.getEntityList();

        List<HashMap<String, Object>> collect = allAction.parallelStream().sorted(Comparator.comparingInt(SysAction::getWeight).reversed()).map(c -> {
            HashMap<String, Object> map = Maps.newHashMap();
            map.put("id", c.getId());
            map.put("action_name", c.getActionName());
            map.put("url", c.getUrl());
            map.put("parent_id", c.getParentId());
            map.put("icon_url", c.getIconUrl());
            map.put("weight", c.getWeight());
            map.put("action_type_enum", c.getActionTypeEnum());
            map.put("http_method", c.getHttpMethod());
            return map;
        }).collect(Collectors.toList());

        return  ResultVo.createBySuccessData(collect);
    }

    @PostMapping("/SysAction/AddAction")
    public ResultVo addAction(@RequestBody @Valid SysActionVo vo){
        SysAction sysAction=new SysAction();
        sysAction.setActionName(vo.getAction_name());
        sysAction.setActionTypeEnum(vo.getAction_type_enum());
        sysAction.setHttpMethod(vo.getHttp_method());
        sysAction.setUrl(vo.getUrl());
        sysAction.setWeight(vo.getWeight());
        sysAction.setIconUrl(vo.getIcon_url());
        sysAction.setCreateTime(new Date());
        sysAction.setParentId(vo.getParent_id());
        if(sysActionService.insert(sysAction)>0){
            return  ResultVo.createBySuccessData("succeed");
        }
        return  ResultVo.createByError("添加失败");
    }

    @PostMapping("/SysAction/DeleteAction")
    public ResultVo deleteAction(@RequestBody List<Integer> ids){

        Integer row= sysActionService.softDeletes(ids);

        return  ResultVo.createBySuccessData(row);
    }

    @PostMapping("/SysAction/UpdateAction")
    public  ResultVo updateAction(@RequestBody @Valid UpdateActionVo vo){
        if(sysActionService.updateField(vo)>0){
            return ResultVo.createBySuccessData("succeed");
        }
        return ResultVo.createByError("编辑失败");
    }

}
